/*
 * @version: 1.0
 * @Author: zhl
 * @Date: 2021-09-14 09:05:14
 * @LastEditors: zhl
 * @LastEditTime: 2021-09-30 17:01:28
 * @Description: 文件实现描述
 */
#include "JsonManager.h"
#include "common.h"
#include "Base64.hpp"
#include "json.hpp"
#include "dlt698_global.h"
#include "protocol_engine_config.h"


using json = nlohmann::json;


JsonManager::JsonManager(){

}

JsonManager::~JsonManager(){

}


void to_json(json& j, const SERIAL_RESPONSE_MESSAGE& p) {
    j = json{
        {"token",p.token}, 
        {"status", p.status}, 
        {"prm", p.prm}, 
        {"port", p.port}, 
        {"data", p.data}, 
        {"timestamp", p.timestamp}
        };
}

void from_json(const json& j, SERIAL_RESPONSE_MESSAGE& p) {
    j.at("token").get_to(p.token);
    j.at("status").get_to(p.status);
    j.at("prm").get_to(p.prm);
    j.at("port").get_to(p.port);
    j.at("data").get_to(p.data);
    j.at("timestamp").get_to(p.timestamp);
}

void to_json(json& j, const SERIAL_REQUEST_MESSAGE& p) {
    j = json{
        {"token", p.token}, 
        {"prio", p.prio}, 
        {"prm", p.prm}, 
        {"taskTimeout", p.taskTimeout}, 
        {"byteTimeout", p.byteTimeout}, 
        {"frameTimeout", p.frameTimeout}, 
        {"port", p.port}, 
        {"estSize", p.estSize}, 
        {"param", {
            {"baudRate", p.param.baudRate}, 
            {"byteSize", p.param.byteSize}, 
            {"parity", p.param.parity}, 
            {"stopBits", p.param.stopBits}}
        }, 
        {"data", p.data}, 
        {"timestamp", p.timestamp}};
}

void from_json(const json& j, SERIAL_REQUEST_MESSAGE& p) {
    j.at("token").get_to(p.token);
    j.at("prio").get_to(p.prio);
    j.at("prm").get_to(p.prm);
    j.at("taskTimeout").get_to(p.taskTimeout);
    j.at("byteTimeout").get_to(p.byteTimeout);
    j.at("frameTimeout").get_to(p.frameTimeout);
    j.at("param").at("baudRate").get_to(p.param.baudRate);
    j.at("param").at("byteSize").get_to(p.param.byteSize);
    j.at("param").at("parity").get_to(p.param.parity);
    j.at("param").at("stopBits").get_to(p.param.stopBits);
    j.at("port").get_to(p.port);
    j.at("estSize").get_to(p.estSize);
    j.at("data").get_to(p.data);
    j.at("timestamp").get_to(p.timestamp);
}

int JsonManager::get_serial_request_msg(std::string addr, int req_id, SERIAL_PARAMS params, char *sendBuf, int buf_len, std::string &msg){
    int tl = get_base64_len(buf_len) + 100;
    char targetBuf[tl] = {0};
    // hex_to_str((unsigned char *)sendBuf, buf_len, (char *)targetBuf, &tl);
    tl = base64_encode(sendBuf, buf_len, targetBuf, tl);
    SERIAL_REQUEST_MESSAGE srmsg;
    srmsg.token = req_id;
    srmsg.prio = 4;
    srmsg.prm = 1;
    srmsg.taskTimeout = 3000;
    srmsg.byteTimeout = 100;
    srmsg.frameTimeout = 3000;
    srmsg.param.baudRate = params.baudRate;
    srmsg.param.byteSize = params.byteSize;
    srmsg.param.parity = params.parity;
    srmsg.param.stopBits = params.stopBits;
    srmsg.data = targetBuf;
    srmsg.estSize = srmsg.data.length();
    get_uart_timestamp(srmsg.timestamp);

    json j = srmsg;
    msg = j.dump();
    return msg.length();
}
//
void JsonManager::get_history_data_obj(int64_t dev_id, HISTORY_DATA &history){
    auto data = g_dev_content.find(dev_id);
    if(data == g_dev_content.end()){
        return ;
    }
    json j;
    int  i = 0;
    for(auto oad = data->second.begin(); oad != data->second.end(); oad++){
        i = 0;
        for(auto oadv : oad->second){
            j[oad->first][std::to_string(i)] = oadv;
            i++;
        }
    }

    g_dev_content.erase(dev_id);
    
    history.dev_id  = dev_id;
    history.dtime   = get_timestamp();
    history.content = j.dump();
    history.is_report = true;
}